package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.provider.mapper.LocalStoreMapper;
import com.weixing.mall.provider.model.domain.LocalStore;
import com.weixing.mall.provider.model.dto.cms.LocalStoreDto;
import com.weixing.mall.provider.model.query.LocalStoreQuery;
import com.weixing.mall.provider.model.query.NearLocalStoreQuery;
import com.weixing.mall.provider.model.vo.LocalStoreEditVo;
import com.weixing.mall.provider.model.vo.LocalStoreNearVo;
import com.weixing.mall.provider.model.vo.LocalStoreVo;
import com.weixing.mall.provider.service.ILocalStoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 实体店铺 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class LocalStoreServiceImpl extends ServiceImpl<LocalStoreMapper, LocalStore> implements ILocalStoreService {


    @Override
    public IPage<LocalStoreVo> selectOfPage(Page page, LocalStoreQuery localStoreQuery) {
        IPage<LocalStoreVo> pageList = baseMapper.selectListOfPage(page,localStoreQuery);
        List<LocalStoreVo> list = pageList.getRecords();
        for(int i=0;i<list.size();i++){
            list.get(i).setStoreAddr(list.get(i).getProvince()+list.get(i).getCity()+list.get(i).getRegion()+list.get(i).getTown()+list.get(i).getStoreAddr());
        }
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<LocalStoreNearVo> selectStoreOfPage(Page page, NearLocalStoreQuery nearLocalStoreQuery) {
        IPage<LocalStoreNearVo> pageList = baseMapper.selectStoreOfPage(page,nearLocalStoreQuery);
        List<LocalStoreNearVo> list = pageList.getRecords();
        for(int i=0;i<list.size();i++){
            list.get(i).setStoreAddr(list.get(i).getProvince()+list.get(i).getCity()+list.get(i).getRegion()+list.get(i).getTown()+list.get(i).getStoreAddr());
        }
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public void save(LocalStoreDto localStoreDto){
        LocalStore localStore = new LocalStore();
        BeanUtils.copyProperties(localStoreDto,localStore);
        String[] areaPath = localStoreDto.getAreaPath();
        localStore.setProvince(areaPath[0]);
        localStore.setCity(areaPath[1]);
        localStore.setRegion(areaPath[2]);
        localStore.setTown(areaPath[3]);
        if(null==localStoreDto.getId()){
            save(localStore);
        }else{
            updateById(localStore);
        }
    }
    @Override
    public void changeEnable(Long id, IsEnableEnum isEnable) {
        LocalStore localStore = new LocalStore();
        localStore.setId(id);
        localStore.setStatus(isEnable.getValue());
        updateById(localStore);
    }
    @Override
    public LocalStoreEditVo selectLocalStoreEditVoById(long id){
        LocalStoreEditVo editVo=baseMapper.selectLocalStoreEditVo(id);
        ArrayList<String> areaPathList = Lists.newArrayList();
        String [] split= new String[]{editVo.getProvince(),editVo.getCity(),editVo.getRegion(),editVo.getTown()};
        for (String s : split) {
            areaPathList.add(s);
        }
        editVo.setAreaPath(areaPathList);
        return editVo;
    }
}
